package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxCopywriterModel;
import com.wwdx.utils.ConstantHolder;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/3/30 11:42
 */
@Controller
@RequestMapping("/system/wwdx_copywriter")
public class WwdxCopywriterController extends BaseController{

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView chapterlist(WwdxCopywriterModel model,
                                    @RequestParam(value = "page", defaultValue = "1") int page,
                                    @RequestParam(value = "rows",defaultValue = "10") int rows) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("listSize",  wwdxCopywriterService.selectModelCount(model));
        mav.addObject("list",  wwdxCopywriterService.selectModelList(model,page,rows));
        return mav;
    }

    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create() {
        return new ModelAndView();
    }

    /**
     * 修改页面
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Long userid) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("model",wwdxCopywriterService.getById(userid));
        return mav;
    }

    /**
     * 保存新增
     */
    @RequestMapping("/save")
    public String save(WwdxCopywriterModel model, HttpServletRequest request){
        this.execute(request,model);
        model.setGmtCreated(new Date());
        model.setGmtModified(new Date());
        wwdxCopywriterService.save(model);
        return "redirect:/system/wwdx_copywriter/list";
    }

    private void execute(HttpServletRequest request,WwdxCopywriterModel model){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        String headImg = "HeadImg";
        fileMap.forEach((k,file)->{
            if (file.getSize() > 0) {
                if (headImg.equalsIgnoreCase(file.getName())) {
                    model.setHeadimg(super.upload(file,directory + ConstantHolder.Constant.FACE));
                }
            }
        });
    }

    /**
     * 修改保存
     */
    @RequestMapping("/update")
    public String update(HttpServletRequest request,WwdxCopywriterModel model){
        this.execute(request,model);
        model.setGmtModified(new Date());
        wwdxCopywriterService.update(model);
        return "redirect:/system/wwdx_copywriter/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public String delete(Long userid) {
        wwdxCopywriterService.delete(wwdxCopywriterService.getById(userid));
        return "redirect:/system/wwdx_copywriter/list";
    }
}